//
//  tempTwoTableViewCell.m
//  XWZ_News_Work
//
//  Created by dllo on 16/1/15.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "tempTwoTableViewCell.h"
#import "orderModel.h"
#import "Order+CoreDataProperties.h"
#import "Order.h"
@interface tempTwoTableViewCell()

@property (nonatomic, strong) UIView *backV;
@property (nonatomic, strong) UIButton *orderBut;

@end

@implementation tempTwoTableViewCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self createSubviews];
    }
    return self;
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    [self.tnameLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(@20);
        make.left.equalTo(@10);
        make.width.equalTo(self.contentView).multipliedBy(0.5);
        make.height.equalTo(@30);
        
    }];
    
    [self.orderCountLab mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(self.tnameLab.mas_bottom).offset(1);
        make.left.equalTo(@10);
        make.width.equalTo(self.contentView).multipliedBy(0.5);
        make.height.equalTo(@20);
        
    }];
    
    [self.titleLab mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(self.orderCountLab.mas_bottom).offset(10);
        make.left.equalTo(@10);
        make.width.equalTo(self.contentView);
        make.height.equalTo(@30);
        
    }];
    
    [self.digistLab mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(self.titleLab.mas_bottom).offset(5);
        make.left.equalTo(@10);
        make.width.equalTo(self.contentView);
        make.height.equalTo(@30);
        
    }];
    
    [self.backV mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.top.equalTo(@5);
        make.left.equalTo(@5);
        make.right.equalTo(self.contentView).offset(-5);
        make.bottom.equalTo(self.contentView).offset(-5);
        
        
    }];
    
    [self.orderBut mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.top.equalTo(@10);
        make.right.equalTo(self.contentView).offset(-10);
        make.width.equalTo(self.contentView).multipliedBy(0.1);
        make.height.equalTo(self.contentView.mas_width).multipliedBy(0.1);
        
    }];
}



- (void)createSubviews
{
    self.backgroundColor = [UIColor colorWithRed:191 / 255.0 green:191 / 255.0 blue:191 / 255.0 alpha:1.0];
    
    self.backV = [UIView new];
    self.backV.backgroundColor = [UIColor whiteColor];
    self.backV.layer.cornerRadius = 5;
    self.backV.layer.masksToBounds = YES;
    [self.contentView addSubview:self.backV];
    
    self.tnameLab = [UILabel new];
    self.tnameLab.font = [UIFont systemFontOfSize:18];
    [self.contentView addSubview:self.tnameLab];
    
    self.orderCountLab = [UILabel new];
    self.orderCountLab.font = [UIFont systemFontOfSize:12];
    self.orderCountLab.textColor = [UIColor grayColor];
    [self.contentView addSubview:self.orderCountLab];
    
    self.titleLab = [UILabel new];
    [self.contentView addSubview:self.titleLab];
    
    self.digistLab = [UILabel new];
    self.digistLab.font = [UIFont systemFontOfSize:14];
    self.digistLab.textColor = [UIColor grayColor];
    [self.contentView addSubview:self.digistLab];
    
    self.orderBut = [UIButton buttonWithType:(UIButtonTypeCustom)];


    [self.orderBut addTarget:self action:@selector(butAction:) forControlEvents:(UIControlEventTouchUpInside)];
    [self.contentView addSubview:self.orderBut];
}
- (void)setMyModel:(orderModel *)myModel
{
    if (_myModel != myModel) {
        _myModel = myModel;
    }
    self.tnameLab.text = myModel.tname;
    self.orderCountLab.text = [NSString stringWithFormat:@"%@人订阅", myModel.subnum];
    self.title = myModel.tname;
    self.titleLab.text = myModel.title;
    self.digistLab.text = myModel.digest;
   
    NSString *str = myModel.tname;
    NSMutableArray *moreArray = [[FMDBSingleton FMDBDataBase] selectReacommendName];
    if ([moreArray containsObject:str])
    {
        self.myModel.isCollect = YES;
        [_orderBut setImage:[UIImage imageNamed:@"iconfont-jianhao"] forState:(UIControlStateNormal)];
    }
     else
        {
            self.myModel.isCollect = NO;
            
            [_orderBut setImage:[UIImage imageNamed:@"iconfont-tianjia"] forState:(UIControlStateNormal)];
        }
    
 
}

- (void)butAction:(UIButton *)sender
{
    if (self.myModel.isCollect)
    {
        [[FMDBSingleton FMDBDataBase] deleteData:self.myModel.tname];
        [self.orderBut setImage:[UIImage imageNamed:@"iconfont-tianjia"] forState:(UIControlStateNormal)];
 
    }
    else
    {
        
           [[FMDBSingleton FMDBDataBase] insertRecommend:self.myModel];
          [self.orderBut setImage:[UIImage imageNamed:@"iconfont-jianhao"] forState:(UIControlStateNormal)];
        
    }
    //    [[NSNotificationCenter defaultCenter] postNotificationName:@"reloadAgain" object:nil];
    
    self.myModel.isCollect = !self.myModel.isCollect;
    //    [self.delegate reloadAgain:self.recommend.tname];
    
    

}


- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
